﻿using Newtonsoft.Json.Linq;
using System;
using System.Net;

namespace scanner_tron_v2
{
    //连入tron 网络，先把所有交易id扒下来

    //v3 继续用交易id 去查地址1
    internal class Program
    {
        static string[] ips ={
    "3.225.171.164",
    "52.53.189.99",
    "18.196.99.16",
    "34.253.187.192",
    "18.133.82.227",
    "35.180.51.163",
    "54.252.224.209",
    "18.231.27.82",
    "52.15.93.92",
    "34.220.77.106",
    "15.207.144.3",
    "13.124.62.58",
    "13.229.128.108",
    "35.182.37.246",
    "34.200.228.125",
    "18.220.232.201",
    "13.57.30.186",
    "35.165.103.105",
    "18.184.238.21",
    "34.250.140.143",
    "35.176.192.130",
    "52.47.197.188",
    "52.62.210.100",
    "13.231.4.243",
    "18.231.76.29",
    "35.154.90.144",
    "13.125.210.234",
    "13.250.40.82",
    "35.183.101.48"};


        const int linecount = 100;
        static async void TaskMain()
        {


            //Console.WriteLine("height=" + Finder.RemoteHeight);

            if (System.IO.Directory.Exists("data") == false)
            {
                System.IO.Directory.CreateDirectory("data");
            }
            if (System.IO.Directory.Exists("dataaddr") == false)
            {
                System.IO.Directory.CreateDirectory("dataaddr");
            }
            if (System.IO.Directory.Exists("index") == false)
            {
                System.IO.Directory.CreateDirectory("index");
            }
            //Console.Clear();

            for (var i = 0; i < linecount; i++)
            {
                var liner = new Liner(i);
                RunLiner(liner, i, GetParsedHeight(i));
            }

            Finder.BeginTestIP(ips);
            LogIPS();
        }
        static async void LogIPS()
        {
            while (true)
            {
                Console.SetCursorPosition(0, 0);
                Console.WriteLine("ips=" + "(used/free/test)" + Finder.usePoints + "/" + Finder.freePoints.Count + "/" + Finder.testips.Count + "  height=" + Finder.RemoteHeight + "    "); ;
                await Task.Delay(500);
            }


        }
        static async void RunLiner(Liner liner, int index, int begin)
        {
            List<string> addrs = new List<string>();
            List<string> txids = new List<string>();
            if (begin % linecount != index)
            {
                throw new Exception("Error Liner.");
            }
            int num = begin;
            while (true)
            {
                addrs.Clear();
                txids.Clear();
                await liner.GetABlock(num, addrs, txids);
                if (addrs.Count != 0)
                {
                    AppendAddr(index, num, addrs);
                }
                if(txids.Count!=0)
                {
                    AppendTx(index,num, txids);
                }
                num += linecount;
                SetParsedHeight(index, num);
                int y = index / 10 + 5;
                int x = index % 10;
                Console.SetCursorPosition(x * 10, y);
                var log = num.ToString();
                while (log.Length < 10)
                    log += " ";
                Console.WriteLine(log);
            }

        }
        //分10条线跑，
        static int[] localheight = null;

        static int GetParsedHeight(int line)
        {
            if (localheight == null)
            {
                localheight = new int[linecount];
                for (var i = 0; i < linecount; i++)
                {
                    try
                    {
                        localheight[i] = int.Parse(System.IO.File.ReadAllLines("index/index_" + i.ToString("D04") + ".txt")[0]);
                    }
                    catch
                    {
                        localheight[i] = i;
                    }
                }
            }
            return localheight[line];
        }
        static void SetParsedHeight(int i, int height)
        {
            localheight[i] = height;
            System.IO.File.WriteAllText("index/index_" + i.ToString("D04") + ".txt", height.ToString() + "\n");
        }
        static void AppendAddr(int line, int height, List<string> result)
        {
            var filename = "dataaddr/" + "addr_line" + line.ToString("D04") + "_" + (height / 1000000) + ".txt";
            System.IO.File.AppendAllLines(filename, result);
        }
        static void AppendTx(int line, int height, List<string> result)
        {
            var filename = "data/" + "tx_line" + line.ToString("D04") + "_" + (height / 1000000) + ".txt";
            System.IO.File.AppendAllLines(filename, result);
        }
        static void Main(string[] args)
        {
            TaskMain();

            while (
                true)
            {
                Console.ReadLine();
            }
        }
    }
}